//
//  TabMainVC.swift
//  ZJTools
//
//  Created by 万孟超 on 2023/10/19.
//

import Cocoa
import SnapKit

class TabMainVC: NSViewController {

    @IBOutlet weak var contentView: NSView!
    @IBOutlet weak var leftCustomView: NSView!
    
    private var currentIndex: Int = 0
    
    lazy var itemArray: [MainItemM] = {
        var itemOne = MainItemM(title: "头文件检测", image: "item1") {[weak self] in
            guard let self = self else { return }
            if self.currentIndex != 0 {
                let options: TransitionOptions = self.currentIndex > 0 ? .slideDown : .slideUp
                self.transition(from: self.children[self.currentIndex], to: self.children[0], options: options)
                self.currentIndex = 0
            }
        }
        
        var itemTwo = MainItemM(title: "Class类清理", image: "item2") {[weak self] in
            guard let self = self else { return }
            if self.currentIndex != 1 {
                let options: TransitionOptions = self.currentIndex > 1 ? .slideDown : .slideUp
                self.transition(from: self.children[self.currentIndex], to: self.children[1], options: options)
                self.currentIndex = 1
            }
        }
        
        var itemThree = MainItemM(title: "Finder文件清理", image: "item3") {[weak self] in
            guard let self = self else { return }
            if self.currentIndex != 2 {
                let options: TransitionOptions = self.currentIndex > 2 ? .slideDown : .slideUp
                self.transition(from: self.children[self.currentIndex], to: self.children[2], options: options)
                self.currentIndex = 2
            }
        }
        
        var toolItem = MainItemM(title: "数据转换工具", image: "item4") {[weak self] in
            guard let self = self else { return }
            if self.currentIndex != 3 {
                let options: TransitionOptions = self.currentIndex > 3 ? .slideDown : .slideUp
                self.transition(from: self.children[self.currentIndex], to: self.children[3], options: options)
                self.currentIndex = 3
            }
        }
        
        var jsonToModelItem = MainItemM(title: "JSON转模型", image: "item5") {[weak self] in
            guard let self = self else { return }
            if self.currentIndex != 4 {
                let options: TransitionOptions = self.currentIndex > 4 ? .slideDown : .slideUp
                self.transition(from: self.children[self.currentIndex], to: self.children[4], options: options)
                self.currentIndex = 4
            }
        }
        
        var xcodeCleanItem = MainItemM(title: "Xcode缓存清理", image: "item6") {[weak self] in
            guard let self = self else { return }
            if self.currentIndex != 5 {
                let options: TransitionOptions = self.currentIndex > 5 ? .slideDown : .slideUp
                self.transition(from: self.children[self.currentIndex], to: self.children[5], options: options)
                self.currentIndex = 5
            }
        }
        
       return [itemOne, itemTwo, itemThree, toolItem, jsonToModelItem, xcodeCleanItem]
    }()
    
    lazy var leftTabView: MainItemV = {
        let tabVie = MainItemV(itemArray: itemArray)
        return tabVie
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.view.wantsLayer = true
        
        view.wantsLayer = true
        view.addGradientBackgroundColor(colors: [.randomColor(alpha: 0.3), .randomColor(alpha: 0.6)], gradientType: .UpRightToBottomLeft, radius: 12, boundsSize: NSMakeSize(1000, 650))
        
        
        leftCustomView.wantsLayer = true
        leftCustomView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
        installSubview()
        installChildView()
    }
    
    func installSubview() {
        leftCustomView.addSubview(leftTabView)
        leftTabView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(15)
            make.size.equalTo(NSMakeSize(200, CGFloat(itemArray.count * 40)))
        }
    }
    
    func installChildView() {
        let fileCheckVC = FileCheckVC(nibName: "FileCheckVC", bundle: nil)
        let classCheckVC = ClassCheckVC(nibName: "ClassCheckVC", bundle: nil)
        let fileCleanVC = FileCleanVC(nibName: "FileCleanVC", bundle: nil)
        let toolsVC = ToolsVC(nibName: "ToolsVC", bundle: nil)
        let jsonToModelVC = JSONToModelVC(nibName: "JSONToModelVC", bundle: nil)
        let xcodeCleanVC = XcodeCleanVC(nibName: "XcodeCleanVC", bundle: nil)
        addChild(fileCheckVC)
        addChild(classCheckVC)
        addChild(fileCleanVC)
        addChild(toolsVC)
        addChild(jsonToModelVC)
        addChild(xcodeCleanVC)
        contentView.addSubview(fileCheckVC.view)
    }
    
    @IBAction func copyUrl(_ sender: NSButton) {
        let pasteboard = NSPasteboard.general
        pasteboard.clearContents()
        pasteboard.setString("git@codehub.devcloud.cn-east-3.huaweicloud.com:IOSgjk_WanMengChao00001/ZJToolsAPP.git", forType: .string)
        NSPopover.showPopover(["复制成功", "使用了Swift Package，第一次打开会下载SnapKit，先等一会儿"], of: sender, position: .minY)
    }
}

